<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | Author: create by allen.qiang
// +----------------------------------------------------------------------



/**
 * 入库单类
 * @package  htdocs
 * @author   create by allen.qiang
 * @version 1.0
 */
class warehouse{

    /**
     * 查询入库单信息
     * @param array $arg_array 参数数组
     * $arg_array = array (
     *       'sortname' => string 排序字段
     *       'sortorder' => string 排序方式
     *       'page' => int 分页页码
     *       'rp' => int 分页页数
     * )
     * @return array|null
     */
    public function searchPutInStoreList(&$arg_array) {

        global $G_SHOP;

        //sql字符串
        $sql = '';

        //参数类型字符串
        $params_type = '';

        //参数值数组
        $params_array = array();

        //入库单号
        if (isset($arg_array['voucherID']) && is_numeric($arg_array['voucherID'])) {
            $sql .= ' AND voucherID = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['voucherID'];
        }

        //入库单编号
        if (isset($arg_array['voucherCode']) && !empty($arg_array['voucherCode'])) {
            $sql .= ' AND voucherCode = ?';
            $params_type .= 's';
            $params_array[] = $arg_array['voucherCode'];
        }

        //创建人
        if (isset($arg_array['creator']) && !empty($arg_array['creator'])) {
            $sql .= ' AND creator LIKE ?';
            $params_type .= 's';
            $params_array[] = '%' . trim($arg_array['creator']) . '%';
        }

        //创建时间
        if (isset($arg_array['startDate']) && !empty($arg_array['startDate'])) {
            $sql .= ' AND createTime >= ?';
            $params_type .= 's';
            $params_array[] = $arg_array['startDate'] . ' 00:00:00';
        }
        if (isset($arg_array['endDate']) && !empty($arg_array['endDate'])) {
            $sql .= ' AND createTime <= ?';
            $params_type .= 's';
            $params_array[] = $arg_array['endDate'] . ' 00:00:00';
        }

        //入库单类型
        if (isset($arg_array['voucherType']) && !empty($arg_array['voucherType'])) {
            $sql .= ' AND voucherType = ?';
            $params_type .= 's';
            $params_array[] = $arg_array['voucherType'];
        }

        if ($sql) {
            $sql = ' WHERE ' . substr($sql, 4);
        }

        try {

            if (isset($arg_array['rp']) && $arg_array['rp'] > 1) {

                //统计分类数量
                $search_sql = 'SELECT COUNT(*) AS item_count FROM wms_warehouse ' . $sql;
                $count_array = &$G_SHOP->DBCA->stmt_query($search_sql, $params_type, $params_array);
                $item_count = $count_array[0]->item_count;
                if ($item_count == 0) {
                    $arg_array['count'] = 0;
                    return null;
                }

                $arg_array['count'] = $item_count;

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

                //分页
                if (!isset($arg_array['page']) || !is_numeric($arg_array['page']) || $arg_array['page'] <= 0)
                    $arg_array['page'] = 1;

                $sql .= sprintf(' LIMIT %d, %d', ($arg_array['page'] - 1) * $arg_array['rp'], $arg_array['rp']);
            } else {

                $arg_array['count'] = 0;

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);
            }

            //查询分类信息
            $search_sql = 'SELECT * FROM wms_warehouse ' . $sql;
            $temp_item_array = &$G_SHOP->DBCA->stmt_query($search_sql, $params_type, $params_array);
            if (!$temp_item_array) {
                return null;
            }

            $item_array = array();
            foreach ($temp_item_array as &$item) {

                $item_array[] = array(
                    'creator' => $item->creator,
                    'createTime' => $item->createTime,
                    'voucherID' => $item->voucherID,
                    'voucherType' => $item->voucherType,
                    'goodsAmount' => $item->goodsAmount,
                    'goodsPrice' => $item->goodsPrice,
                    'storeName' => $item->storeName,
                    'storeID' => $item->storeID,
                    'auditFlg' => $item->auditFlg,
                    'auditor' => $item->auditor,
                    'auditTime' => $item->auditTime,
                    'voucherCode' => $item->voucherCode,
                    'remark' => $item->remark
                );
            }

            return $item_array;

        } catch (Exception $e) {

            error_log($e->getMessage());
            return null;
        }

    }

    /**
     * 保存入库单
     */
    public function savePutInStore( $data_array ) {

        global $G_SHOP;

        if (!isset($data_array['voucherType'], $data_array['storeID'], $data_array['operator'], $data_array['goods'])) {
            return array('code' => 400, 'message' => '缺少参数');
        }

        if (!in_array($data_array['voucherType'], array(1, 2, 3))) {
            return array('code' => 400, 'message' => '请选择入库单类型');
        }

        if (!quanli_is_int($data_array['storeID'])) {
            return array('code' => 400, 'message' => '请选择仓库');
        }

        if (count($data_array['goods']) == 0) {
            return array('code' => 400, 'message' => '请录入商品');
        }

        //加载订单类、仓库类
        quanli_include('module/base/bill/billSearch.class.php');
        quanli_include('module/base/wms/wmsStore.class.php');

        //实例订单类、仓库类
        $billSearch = new billSearch;
        $wmsStore = new wmsStore;

        try {

            $G_SHOP->DBCA->beginTransaction();

            //检查仓库信息
            $store_array = $wmsStore->searchStoreDetail( $data_array['storeID'] );
            if (!$store_array) {
                throw new Exception('仓库信息没有找到', 404);
            }

            $voucherID = generate_seqno('seqno_warehouse'); //入库单ID
            $total_goods_amount = 0; //入库商品总数量
            $total_goods_price = 0; //入库商品总金额

            foreach ($data_array['goods'] as $goods) {

                //根据订单号和订单流水号检查订单商品信息
                $bill_args_array = array(
                    'bill_no' => $goods['bill_no'],
                    'bill_goods_no' => $goods['bill_goods_no']
                );
                $bill_goods_array = $billSearch->searchBillGoods( $bill_args_array );
                if (!$bill_goods_array) {
                    throw new Exception('订单商品编号[' . $goods['bill_goods_no'] . ']，订单商品信息没有找到', 404);
                }
                $bill_goods_array = $bill_goods_array[0];

                //检查入库单商品是否已在其他入库单中

                //入库商品信息
                $wms_warehouse_goods = new entity('wms_warehouse_goods');
                $wms_warehouse_goods->creator = $data_array['operator'];
                $wms_warehouse_goods->voucherID = $voucherID;
                $wms_warehouse_goods->bill_no = $bill_goods_array['bill_no'];
                $wms_warehouse_goods->bill_goods_no = $bill_goods_array['bill_goods_no'];
                $wms_warehouse_goods->postID = $bill_goods_array['postID'];
                $wms_warehouse_goods->goodsID = $bill_goods_array['goodsID'];
                $wms_warehouse_goods->product_code = $bill_goods_array['product_code'];
                $wms_warehouse_goods->goodsName = $bill_goods_array['goodsName'];
                $wms_warehouse_goods->goodsSpec = $bill_goods_array['spec'];
                $wms_warehouse_goods->goodsPrice = $bill_goods_array['price'];
                $wms_warehouse_goods->goodsAmount = 1;
                $G_SHOP->DBCA->save($wms_warehouse_goods);

                $total_goods_amount++;
                $total_goods_price += $bill_goods_array['price'];

            }

            $wms_warehouse = new entity('wms_warehouse');
            $wms_warehouse->voucherID = $voucherID;
            $wms_warehouse->voucherCode = 'RK' . $voucherID;
            $wms_warehouse->voucherType = $data_array['voucherType'];
            $wms_warehouse->creator = $data_array['operator'];
            $wms_warehouse->goodsAmount = 1;//订单商品数量默认为 1
            $wms_warehouse->goodsPrice = $total_goods_price;
            $wms_warehouse->storeID = $store_array['storeID'];
            $wms_warehouse->storeName = $store_array['storeName'];
            $wms_warehouse->auditFlg = 0;
            $G_SHOP->DBCA->save($wms_warehouse);

            $G_SHOP->DBCA->commit();

            return array('code' => 200, 'message' => 'ok');

        } catch (Exception $e) {

            $G_SHOP->DBCA->rollback();

            if ($e->getCode() > 0) {
                return array( 'code' => $e->getCode(), 'message' => $e->getMessage() );
            } else {
                error_log($e->getMessage());
                return array('code' => 500, 'message' => '系统错误');
            }

        }

    }

    public function checkGoodsIsInWarehouse( $bill_no, $bill_goods_no ) {



    }

}